module manager {

    export class SoundManager {

        private static soundMap: any = {};

        private static soundChannelMap: any = {};

        public static playSound(resKey: string, isSaveChannel: boolean=false, startAt: number = 0, loopTimes: number = 1): egret.SoundChannel {
            let gameMnager: GameManager = GameManager.getInstance();

            if (gameMnager.getSetting().getMusicEnabled() == false) {
                this.stopSound('resource/assets/GameRes/game/music/bgMusic.mp3')
                return;
            }

            if (SoundManager.soundMap[resKey] != null) {
                let sound: egret.Sound = SoundManager.soundMap[resKey];
                if (sound != null) {
                    if (isSaveChannel) {
                        SoundManager.soundChannelMap[resKey] = sound.play(startAt, loopTimes);
                        return SoundManager.soundChannelMap[resKey];
                    } else {
                        return sound.play(startAt, loopTimes);
                    }
                }
            } else {
                RES.getResAsync(resKey, function (value: any, key: string) {
                    SoundManager.soundMap[resKey] = value;
                    let sound: egret.Sound = SoundManager.soundMap[resKey];
                    if (sound != null) {
                        if (isSaveChannel) {
                            SoundManager.soundChannelMap[resKey] = sound.play(startAt, loopTimes);
                            return SoundManager.soundChannelMap[resKey];
                        } else {
                            return sound.play(startAt, loopTimes);
                        }
                    }
                }, null);
            }
        }

        public static playResLazy(lazytime: number, key: string): void {
            let timeoutId = egret.setTimeout(function (): void {
                SoundManager.playSound(key, false);
                egret.clearTimeout(timeoutId);
            }, this, lazytime);
        }

        public static stopSound(soundKey: string): void {
            if (SoundManager.soundChannelMap.hasOwnProperty(soundKey)
                && SoundManager.soundChannelMap[soundKey] != null) {
                let channel: egret.SoundChannel = <egret.SoundChannel> SoundManager.soundChannelMap[soundKey];
                channel.stop();
            }
        }

        public playSound(soundKey: string): void {
            let gameMnager: GameManager = GameManager.getInstance();

            if (gameMnager.getSetting().getMusicEnabled()) {
                return;
            }
            //创建 Sound 对象
            let sound = new egret.Sound();
            let url: string = "resource/sounds/" + soundKey + ".mp3";
            //添加加载完成侦听
            sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
            //开始加载
            sound.load(url);
        }

        private onLoadComplete(event: egret.Event): void {
            //获取加载到的 Sound 对象
            let sound: egret.Sound = <egret.Sound>event.target;
            //播放音乐
            let channel: egret.SoundChannel = sound.play(0, 1);
            channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);
        }

        private onSoundComplete(event: egret.Event): void {
        }
    }

}